package gw.com.android.ui.onlive;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;

/**
 * 一键晒单，高度为固定，宽度根据比例来适配
 *
 * @author jett
 * @since 2018-03-07.
 */
public class RatioViewPager extends ViewPager {

    private float mRatio = -1;

    public RatioViewPager(Context context) {
        super(context);
    }

    public RatioViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    void setRatio(float ratio) {
        this.mRatio = ratio;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = MeasureSpec.getSize(heightMeasureSpec);
        if (mRatio != -1) {
            int width = (int) (height * mRatio);
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

}
